package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.domain.Orders;
import com.example.result.JsonResult;
import com.example.service.OrdersService;
import com.example.untils.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    //下单
    @PostMapping("/submit")
    public JsonResult submit(@RequestBody Orders orders){
        ordersService.submit(orders);
        return new JsonResult(1,"下单成功");
    }
    //获取订单列表
    @GetMapping("/userPage")
    public JsonResult userPage(Integer page,Integer pageSize){
        Long currentId = BaseContext.getCurrentId();
        log.info("用户id:"+currentId);
        IPage<Orders> iPage=new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(currentId!=null,Orders::getUserId,currentId);
        queryWrapper.orderByDesc(Orders::getOrderTime);
        ordersService.page(iPage,queryWrapper);
        return new JsonResult(1,"",iPage);
    }
    //获取订单详情分页
    @GetMapping("/page")
    public JsonResult page(Integer page,Integer pageSize,String number,String beginTime,String endTime){
        log.info(beginTime+"到"+endTime);
        IPage<Orders> iPage=new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(Strings.isNotEmpty(number),Orders::getNumber,number);
        queryWrapper.ge(Orders::getOrderTime,beginTime);
        queryWrapper.lt(Orders::getOrderTime,endTime);
        queryWrapper.orderByDesc(Orders::getOrderTime);
        ordersService.page(iPage,queryWrapper);

        return new JsonResult(1,"",iPage);
    }
}
